const initialState = {
    arr:[ ],
    duoarr:[],
    arrcopy:[],
    allchecked:false,// 所有的题都做完的时候为true,
    allduochecked:false

}
// 点每一道题目的每一个选项的时候
    // 1. 我们答案里选的是哪一项
    // 2. 要知道当前这道题做过还是没做过
    // 3. 要知道当前这道题做的对不对
    // 4. 整套题做完没做完
export default (state = initialState, { type, payload,index}) => {
  switch (type) {

  case "getlist":

     let newARR = [...state.arr]
     let newARRCOPY =  [...state.arrcopy]
     newARR = payload
     newARRCOPY= payload
    return { ...state, arr:newARR ,arrcopy:newARRCOPY}
  case "getduolist" :
    let newduoARR = JSON.parse(JSON.stringify(state.duoarr))
    newduoARR = payload
    return {
      ...state,
      duoarr:newduoARR
    }
  case "radioFn" :
       let newallchecked = state.allchecked
       let copynewarr = JSON.parse(JSON.stringify(state.arrcopy))
       //我们答案里选的是哪一项
       copynewarr.forEach( item => {
            item.answer.forEach(element => {
                 if (element.id ===  payload) {
                     element.checked = !element.checked
                 }
            });
       });
       // 只要选过就代表着当前的这道题做过了
       copynewarr.forEach( item => {
        item.answer.forEach(element => {
            if (element.checked) {
              item.flag = true
            }
        });
       });
     // 要判断这道题做的对不对
     console.log(index);
     copynewarr.forEach( item => {
      item.answer.forEach(element => {
         if (index == item.rightkey) {
           item.zhen = true
         }
      });
     });
     //  判断这一份题做完还是没有做完
    newallchecked = copynewarr.every((item) => {
        return item.flag
     })
      return {
          ...state,
          arr:copynewarr,
          arrcopy:copynewarr,
          allchecked:newallchecked
      }

  case "checkboxFn":
     let checkduonewArr = JSON.parse(JSON.stringify(state.duoarr))
     // 让当前选中的复选框的布尔值在数据中改变且将当前选中的下标push进我们要比较做的对不对的数组中
     checkduonewArr.forEach( item => {
      item.answer.forEach(element => {
           if (element.id ===  payload) {
               element.checked = !element.checked
               item.pushs.push(index)
           }
      });
    });
      // 只要选过就代表着当前的这道题做过了
      checkduonewArr.forEach( item => {
        item.answer.forEach(element => {
            if (element.checked) {
              item.flag = true
            }
        });
       });
     // 要判断这道题做的对不对了
     checkduonewArr.forEach((item)=>{
         let aaa = item.rightkey.toString()
         let bbb = item.pushs.toString()
         if (aaa === bbb && item.rightkey.length === item.pushs.length) {
              item.zhen = true
         } 

     })
     let newallquanchecked = checkduonewArr.every((item) => {
      return item.flag
   })

 return {
   ...state,
   duoarr:checkduonewArr,
   allduochecked:newallquanchecked

 }
  default:
    return state
  }
}
